package com.woniu.system.aop;


import com.woniu.system.service.ICompanyManager;
import com.woniu.system.service.imp.CompanyManager;
import net.sf.ehcache.Ehcache;
import net.sf.ehcache.Element;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.hibernate.stat.EntityStatistics;
import org.hibernate.stat.Statistics;

import javax.annotation.Resource;
import java.lang.reflect.InvocationTargetException;

/**
 * Created by IntelliJ IDEA.
 * User: xujin
 * Date: 2010-3-11
 * Time: 20:34:37
 * To change this template use File | Settings | File Templates.
 */

@Aspect
public class CompanyAop {
    private static final Log logger = LogFactory.getLog(CompanyManager.class);

    @Resource
    private Ehcache ehcache;

    @Around("execution(* com.woniu.system.service.ICompanyManager.getCompany(..))")
    public Object getCompany(ProceedingJoinPoint joinPoint) throws Throwable {
        Object[] objArray = joinPoint.getArgs();

        if (objArray == null || objArray.length != 1) {
            throw new Exception("没有参数");
        }


        Object obj = this.ehcache.get(objArray[0]);
        if (obj == null) {
            obj = joinPoint.proceed(new Object[]{objArray[0]});
            this.ehcache.put(new Element(objArray[0], obj));
            System.out.println("ddd");
        } else {
            Element element = (Element) obj;
            obj = element.getValue();
        }
        return obj;
    }

    @AfterReturning(pointcut = "execution(* com.woniu.system.service.ICompanyManager.save(..))", returning = "retVal")
    public void save(JoinPoint joinPoint, Object retVal) throws InvocationTargetException, NoSuchMethodException, IllegalAccessException {
        System.out.println(retVal);


        Object objId = Long.parseLong(BeanUtils.getProperty(retVal, "NId"));

        Object obj = this.ehcache.get(objId);
        if (obj != null) {
            this.ehcache.remove(objId);
        }

        this.ehcache.put(new Element(objId, retVal));

        System.out.println("save info");
    }


    @After("execution(* com.woniu.system.service.ICompanyManager.delete(..))")
    public void delete(JoinPoint joinPoint) {
        Object[] objArray = joinPoint.getArgs();
        Object obj = this.ehcache.get(objArray[0]);
        if (obj != null) {
            this.ehcache.remove(objArray[0]);
        }

    }

    @AfterReturning("execution(* com.woniu.system.service.ICompanyManager.findAll(..))")
    public void findAll(JoinPoint joinPoint) {
        ICompanyManager companyManager = (ICompanyManager) joinPoint.getTarget();

        Statistics statistics = companyManager.getDAOHelper().getSession().getSessionFactory().getStatistics();

        // 统计entity信息
        for (int i = 0; i < statistics.getEntityNames().length; i++) {
            String entityName = statistics.getEntityNames()[i];
            System.out.println("entityName:" + entityName);
            EntityStatistics entityStatistics = statistics
                    .getEntityStatistics(entityName);
            System.out.println("entityStatistics:" + entityStatistics);
        }
        System.out.println("finddd");
    }

    @AfterThrowing("execution(* com.woniu.system.service.ICompanyManager.*(..))")
    public void excep(JoinPoint joinPoint) {
        joinPoint.getArgs();
       
        logger.debug("asdfasdf");
    }
}
